// ResultObject.cpp: implementation of the ResultObject class.
//
//////////////////////////////////////////////////////////////////////

#include "ResultObject.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
void ResultObject::resetCurrent()
{
	currentGeneretedNodes = 0;
	bFoundSolution = false;
	currentDepth = 0;
	m_dblSearchTime = 0;
	m_currentTotalCut = 0;
	m_currentJumps = 0;
	m_currentExpended = 0;
}

ResultObject::ResultObject()
{
	m_CurrentOutputName = NULL;
	m_parm = NULL;
	resetCurrent();
	ResetGlobal();
}

ResultObject::~ResultObject()
{

}

void ResultObject::PrintCurent()
{
	printf("%d %d %I64d %I64d %f %I64d\n",m_gbl_TotoalRuns,currentDepth,currentGeneretedNodes, m_currentTotalCut, m_dblSearchTime , m_currentJumps);
	if (m_CurrentOutputName)
	{
		FILE *filePtr;

		filePtr = fopen(m_CurrentOutputName,"r");
		if (filePtr == NULL)
		{
			filePtr = fopen(m_CurrentOutputName,"a+");
			fprintf(filePtr,"Problem\tDepth\tNodes\tCut\tTime\tJumps\n");
		}
		fclose(filePtr);

		filePtr = fopen(m_CurrentOutputName,"a+");
		fprintf(filePtr,"%d\t%d\t%I64d\t%I64d\t%f\t%I64d\n",m_gbl_TotoalRuns + m_skipProblem + 1, currentDepth,currentGeneretedNodes, m_currentTotalCut, m_dblSearchTime, m_currentJumps);
		fclose(filePtr);
	}
}

void ResultObject::SetSolveTime(double dTime)
{
	m_dblSearchTime = dTime;
}

void ResultObject::AddToGlobalResults()
{
	m_gbl_TotoalRuns++;
	m_gblDepth += currentDepth;
	m_gblGeneretedNodes += currentGeneretedNodes;
	m_gblSearchTime += m_dblSearchTime;
	m_gblTotalCut += m_currentTotalCut;
	m_gblTotalJumps  += m_currentJumps;
	m_gblExpended += m_currentExpended ;
}

void ResultObject::PrintGlobalResults()
{
	printf("Total Solved    = %d\n",m_gbl_TotoalRuns);
	printf("Ave. Depth      = %.3f\n", (double)m_gblDepth/(double)m_gbl_TotoalRuns);
	printf("Ave. Generated  = %.3f\n",(double)m_gblGeneretedNodes/(double)m_gbl_TotoalRuns);
	printf("Ave. Expended   = %.3f\n",(double)m_gblExpended/(double)m_gbl_TotoalRuns);
	printf("Ave. SearchTime = %.3f\n",(double)m_gblSearchTime/(double)m_gbl_TotoalRuns);
	printf("Ave. Cut        = %.3f\n",(double)m_gblTotalCut/(double)m_gbl_TotoalRuns);
	printf("Ave. Jumps        = %.3f\n",(double)m_gblTotalJumps/(double)m_gbl_TotoalRuns);

	if (m_CurrentOutputName)
	{
		FILE *filePtr;
		filePtr = fopen(m_CurrentOutputName,"a+");
		fprintf(filePtr,"Total Solved    = %d\n",m_gbl_TotoalRuns);
		fprintf(filePtr,"Ave. Depth      = %.3f\n", (double)m_gblDepth/(double)m_gbl_TotoalRuns);
		fprintf(filePtr,"Ave. Generated  = %.3f\n",(double)m_gblGeneretedNodes/(double)m_gbl_TotoalRuns);	
		fprintf(filePtr,"Ave. Expended  = %.3f\n",(double)m_gblExpended/(double)m_gbl_TotoalRuns);	
		fprintf(filePtr,"Ave. SearchTime = %.3f\n",(double)m_gblSearchTime/(double)m_gbl_TotoalRuns);
		fprintf(filePtr,"Ave. Cut        = %.3f\n",(double)m_gblTotalCut/(double)m_gbl_TotoalRuns);
		fprintf(filePtr,"Ave. Jumps        = %.3f\n",(double)m_gblTotalJumps/(double)m_gbl_TotoalRuns);
		fclose(filePtr);
	}
}

void ResultObject::ResetGlobal()
{
	m_gbl_TotoalRuns = 0;
	m_gblDepth = 0 ;
	m_gblGeneretedNodes = 0;
	m_gblSearchTime = 0;
	m_gblTotalCut = 0;
	m_skipProblem = 0;
	m_gblTotalJumps = 0;
	m_gblExpended = 0;
}

void ResultObject::SetParameters(Parameters *in_parm)
{
	m_parm = in_parm;
}


void ResultObject::PrintHeuristicDistributionArray(I64 HeuristicsValuesTable[40])
{
	int maxValue=0;
	FILE *filePtr;
	filePtr = fopen("RunTimeDistribution.txt","a+");
	fprintf(filePtr,"Heuristic\tNodes\n");


	for (int i = 0; i<40;i++)
	{
		if (HeuristicsValuesTable[i] > 0)
			maxValue = i;
	}

	for (i = 0; i<=maxValue;i++)
	{
		printf("%d\t%I64d\n", i, HeuristicsValuesTable[i]);
		fprintf(filePtr, "%d\t%I64d\n", i, HeuristicsValuesTable[i]);
	}

	fclose(filePtr);
}

